<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Part extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['part_addtime','part_uptime','part_uid'];
	protected $update = ['part_uptime'];		
	protected function setPartAddtimeAttr($value,$data){
		if (!empty($data['part_checktime']) || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}
	protected function setPartUptimeAttr($value){
		return time();
	}	
    protected function setPartPrtyAttr($value){
        if(!is_array($value)){
            return 0;
        }else{
            $attribute = 0;
            foreach ($value as $key=>$value){
                $attribute += $value;
            }
            return $attribute;
        }
    }	
	protected function setPartContentAttr($value){
	    if(!empty($value)){
	       return zanpian_content_down_images(trim($value),'part');
	    }else{
	       return ""; 	 
	    } 
	}
	protected function setPartUidAttr($value){
		if($value){
			return $value;
		}else{
			return '';
		}
	}	
	//事件注册
    protected static function init(){	
		//写入后置事件
		Part::afterWrite(function ($part){
			if(!empty($part->part_id)){
			   model('Manage')->DelCache(12,$part->part_id);
			}
		 
		});		
		//删除后置操作
        Part::afterDelete(function ($part) {
			$story_data['story_vid']=$part['part_vid'];
			$story_data['story_continu']='';
			$story_data['story_isend']='';
			$story_data['story_name']='';
			model('story')->save($story_data,array('story_vid'=>$part['part_vid']));			
			//删除内容图片
			zanpian_del_img_file($part['part_content']);
			model('Manage')->DelCache(12,$part->part_id);
			
       });	
    }
    public function add_story($post=''){
        $story=db('story')->where('story_vid',$post['story_vid'])->find();
		$story_data['story_vid']=$post['story_vid'];
		$story_data['story_content']=(string)$post['story_content'];
		$story_data['story_continu']=(string)$post['story_continu'];
		$story_data['story_isend']=(string)$post['story_isend'];
		$story_data['story_name']=(string)$post['story_name'];
        $story_data['story_reurl']=(string)$post['story_reurl'];		
		if(empty($story['story_id'])){
			$story_data['story_cid']=$post['story_cid'];
			$story_data['story_status']=$post['story_status'];			
			model('story')->allowField(true)->data($story_data,true)->isUpdate(false)->save();
        }else{
			$story_data['story_id']=$story['story_id'];
			$story_data['story_addtime']=date('Y-m-d H:i:s',time());
			model('story')->allowField(true)->data($story_data,true)->isUpdate(true)->save();	
		}
    }		
    public function count($vid=''){
		if(!empty($vid)){
		      return db('part')->where('part_vid',$vid)->count('part_id');
		}
        
    }		
    public function vod(){
        return $this->hasOne('vod','vod_id','part_vid');
    }
    public function story(){
        return $this->hasOne('story','story_vid','part_vid');
    }
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'part.*,story_id,story_cid,story_vid,story_prty,story_continu,story_isend,vod_id,vod_cid,vod_mcid,vod_name,vod_aliases,vod_title,vod_color,vod_actor,vod_director,vod_content,vod_pic,vod_bigpic,vod_diantai,vod_tvcont,vod_area,vod_language,vod_year,vod_continu,vod_total,vod_isend,vod_addtime,vod_hits,vod_hits_day,vod_hits_week,vod_hits_month,vod_hits_lasttime,vod_stars,vod_up,vod_down,vod_play,vod_gold,vod_golder,vod_isfilm,vod_filmtime,vod_length,vod_weekday,vod_letters,vod_bigpic,vod_jumpurl';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='part_'.$param['order'].' desc';
		}			
		if(empty(order_check($tag['order']))){
			$tag['order']='part_oid asc';
		}		
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		if(empty($tag['status'])) {
		        $where['part_status']=['eq',1];
		}else{
			if($tag['status']==2){
			   $where['part_status']=['eq',0];
			}
		}
	    if(!empty($tag['ids'])){
		     $where['part_id'] = array('in',$tag['ids']);
	    }		
	    if(!empty($tag['sid'])){
		    $vid = explode(',',$tag['sid']);
		    if(count($vid)>1) {
			    $where['story_id'] = array('in',$tag['vid']);
		    }else{
			    $where['story_id'] = array('eq',$vid[0]);
		    }			
	    }
	    if(!empty($tag['vid'])){
		    $vid = explode(',',$tag['vid']);
		    if(count($vid)>1) {
			    $where['part_vid'] = array('in',$tag['vid']);
		    }else{
			    $where['part_vid'] = array('eq',$vid[0]);
		    }			
	    }
		if(!empty($tag['sort'])){
		    $sort = explode(',',$tag['sort']);
		    if(count($sort)>1) {
			    $where['part_oid'] = array('in',$tag['sort']);
		    }else{
			    $where['part_oid'] = array('eq',$sort[0]);
		    }
	    }
	    if(!empty($tag['not'])){
		    $not = explode(',',$tag['not']);
		    if(count($not)>1) {
			    $where['part_id'] = array('not in',$tag['not']);
		    }else{
			    $where['part_id'] = array('neq',$not[0]);
		    }			
	    }
	    if(!empty($tag['vnot'])){
		    $vnot = explode(',',$tag['vnot']);
		    if(count($vnot)>1) {
			    $where['vod_id'] = array('not in',$tag['vnot']);
		    }else{
			    $where['vod_id'] = array('neq',$vnot[0]);
		    }	
	    }		
	    if (!empty($tag['letter'])) {
		    $letter = explode(',',$tag['letter']);
		    if(count($letter)>1) {
			    $where['vod_letter'] = array('in',$tag['letter']);
		    }else{
			    $where['vod_letter'] = array('eq',$letter[0]);
		    }
	    }		
	    if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['story_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['story_cid'] = getlistsqlin($tag['cid']);
		    }
	    }
	    if(!empty($tag['vcid'])) {
		    $cids = explode(',',trim($tag['vcid']));
		    if(count($cids) > 1) {
			    $where['vod_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['vod_cid'] = getlistsqlin($tag['vcid']);
		    }
	    }		
	    if(!empty($tag['stars'])) {
		     $where['part_stars'] = array('in',$tag['stars']);
	    }
	    if(!empty($tag['vstars'])) {
		     $where['vod_stars'] = array('in',$tag['vstars']);
	    }		
	    if (!empty($tag['hits'])) {
		    $hits = explode(',',$tag['hits']);
		    if (count($hits) > 1) {
			    $where['part_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['part_hits'] = array('gt',$hits[0]);
		    }
	    }
	    if (!empty($tag['vhits'])) {
		    $hits = explode(',',$tag['vhits']);
		    if (count($hits) > 1) {
			    $where['vod_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['vod_hits'] = array('gt',$hits[0]);
		    }
	    }		
	    if(!empty($tag['name'])) {
		    $where['vod_name'] = array('like','%'.$tag['name'].'%');	
	    }
	    if(!empty($tag['wd'])) {
		    $where['vod_name'] = array('like','%'.$tag['wd'].'%');	
	    }
        if(!empty($tag['gold'])) {
		    $gold = explode(',',$tag['gold']);
		    if(count($gold) > 1) {
			    $where['part_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['part_gold'] = array('gt',$gold[0]);
		    }
	    }
        if(!empty($tag['vgold'])) {
		    $gold = explode(',',$tag['vgold']);
		    if(count($gold) > 1) {
			    $where['vod_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['vod_gold'] = array('gt',$gold[0]);
		    }
	    }		
	    if(!empty($tag['golder'])) {
		    $golder = explode(',',$tag['golder']);
		    if(count($golder) > 1) {
			    $where['part_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['part_golder'] = array('gt',$golder[0]);
		    }
	    }
	    if(!empty($tag['vgolder'])) {
		    $golder = explode(',',$tag['vgolder']);
		    if(count($golder) > 1) {
			    $where['vod_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['vod_golder'] = array('gt',$golder[0]);
		    }
	    }		
	    if(!empty($tag['up'])) {
		    $up = explode(',',$tag['up']);
		    if(count($up)>1) {
			    $where['part_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['part_up'] = array('gt',$up[0]);
		    }
	    }
	    if(!empty($tag['vup'])) {
		    $up = explode(',',$tag['vup']);
		    if(count($up)>1) {
			    $where['vod_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['vod_up'] = array('gt',$up[0]);
		    }
	    }		
	    if(!empty($tag['down'])) {
		    $down = explode(',',$tag['down']);
		    if(count($down)>1) {
			    $where['part_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['part_down'] = array('gt',$down[0]);
		    }
	    }
	    if(!empty($tag['vdown'])) {
		    $down = explode(',',$tag['vdown']);
		    if(count($down)>1) {
			    $where['vod_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['vod_down'] = array('gt',$down[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['part_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['part_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }
        if(!empty($tag['uptime'])){
		    $time = explode(',',$tag['uptime']);
		    if(count($time) > 1) {
			    $where['part_uptime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['part_uptime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }		
        if(!empty($tag['vaddtime'])){
		    $time = explode(',',$tag['vaddtime']);
		    if(count($time) > 1) {
			    $where['vod_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['vod_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }		
		if($tag['prty']){
			if(!empty(check_position($tag['prty']))){
			    $where['part_prty'] = check_position($tag['prty']);
			}
		}
		if($tag['vprty']){
			if(!empty(check_position($tag['vprty']))){
			    $where['vod_prty'] = check_position($tag['vprty']);
			}
		}
		if($tag['sprty']){
			if(!empty(check_position($tag['sprty']))){
			    $where['story_prty'] = check_position($tag['sprty']);
			}
		}
	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['part_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['part_uid'] = array('eq',$uids[0]);
		    }
	    }else{
			$where['vod_status']=['eq',1];
		}		
        if(!empty($tag['page'])){
			if(!empty($tag['uid'])) {
			    $count=db('part')->where($where)->count('part_id');
			}else{
				
			}
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('part')->alias('part')->join('vod vod','vod.vod_id =part_vid','LEFT')->join('story story','story.story_vid =part_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],$count,['path'=>$url]);
		}else{
			$data = db('part')->alias('part')->join('vod vod','vod.vod_id =part_vid','LEFT')->join('story story','story.story_vid =part_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->limit($tag['limit'])->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_part')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 	
}